package cn.colins.producer.service;

import cn.colins.api.CallbackListener;
import cn.colins.api.CallbackService;
import org.apache.dubbo.config.annotation.Argument;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;

/**
 * @program: dubbo-annotation-study
 * @description:
 * @author: colins
 * @create: 2023-05-06 21:03
 **/
@DubboService(protocol = {"dubbo"},methods = {
        @Method(name = "addListener", arguments = { @Argument(callback = true, type = "cn.colins.api.CallbackListener") })
})
public class CallbackTestServiceImpl implements CallbackService {


    @Override
    public String addListener(String key, CallbackListener listener) {
        // 异步处理
        new Thread(()->{
            listener.changed(getChanged(key)); // 发送变更通知
        }).start();
        // 直接返回
        return key;
    }

    private String getChanged(String key) {
        // 逻辑处理。。。。。完后返回
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "处理了5s返回结果";
    }
}
